﻿import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:characters/characters.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import '../../../gen/assets.gen.dart';
import '../../../entity/daily_check_in_data.dart';

/// 今日签页面
class DailyCheckInView extends StatelessWidget {
  const DailyCheckInView({super.key});

  /// 获取传递的数据
  DailyCheckInData? get _data {
    final arguments = Get.arguments;
    if (arguments != null && arguments is Map<String, dynamic>) {
      return DailyCheckInData.fromJson(arguments);
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Stack(
        children: [
          // 背景渐变 - 最底层
          Container(
            width: double.infinity,
            height: double.infinity,
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
                  Color(0xFF1A1A1A),
                  Color(0xFF000000),
                ],
              ),
            ),
          ),

          // 主要内容卡片 - 中间层
          SafeArea(
            child: Column(
              children: [
                // 顶部关闭按钮
                Container(
                  padding: EdgeInsets.only(left: 10.w),
                  child: Align(
                    alignment: Alignment.centerLeft,
                    child: InkWell(
                      onTap: () {
                        Get.back();
                      },
                      borderRadius: BorderRadius.circular(25.r),
                      child: Container(
                        width: 40.w,
                        height: 40.w,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(25.r),
                          color: Colors.white.withOpacity(0.1),
                        ),
                        child: const Icon(
                          Icons.close,
                          color: Colors.white,
                          size: 20,
                        ),
                      ),
                    ),
                  ),
                ),

                SizedBox(height: 20.h),

                // 主要内容卡片
                Expanded(
                  child: Stack(
                    clipBehavior: Clip.none, // 允许子组件超出边界
                    children: [
                      // 卡片背景图片
                      Center(
                        child: Container(
                          width: double.infinity,
                          height: double.infinity,
                          padding: EdgeInsets.all(10.w),
                          child: Stack(
                            children: [
                              // textBg背景图片
                              Assets.signature.textBg.image(
                                width: double.infinity,
                                height: double.infinity,
                                fit: BoxFit.contain,
                              ),

                              Positioned(
                                top: 30.h,
                                left: 30.w,
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    // 标题
                                    Assets.signature.todayText.image(
                                      width: 114.w,
                                      fit: BoxFit.contain,
                                    ),

                                    SizedBox(height: 8.h),

                                    // 日期
                                    Text(
                                      _getTodayDate(),
                                      style: TextStyle(
                                        color: Colors.grey[400],
                                        fontSize: 13.sp,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),

                      // 右上角装饰标签
                      Positioned(
                        top: -30.h, // 进一步调整top值
                        right: 20.w, // 调整right值，确保完全可见
                        child: Container(
                          width: 66.w, // 增加宽度
                          height: 196.h, // 增加高度
                          child: Stack(
                            children: [
                              // 装饰标签背景图片
                              Assets.signature.luckyBg.image(
                                width: 66.w,
                                height: 196.h,
                                fit: BoxFit.cover,
                              ),
                              // AZring图标 - 定位在标签顶部
                              Positioned(
                                top: 40.h,
                                left: 15.w,
                                child: Assets.signature.aZring.image(
                                  width: 36.w,
                                  height: 7.h, // 恢复原来的高度
                                  fit: BoxFit.cover, // 恢复原来的fit
                                ),
                              ),
                              // 幸运文字 - 定位在标签中央
                              // Positioned(
                              //   top: 10.h,
                              //   left: 5.w, // 稍微向左偏移，确保文字居中
                              //   child: Assets.signature.luckyText.image(
                              //     width: 70.w,
                              //     height: 174.h, // 恢复原来的高度
                              //     fit: BoxFit.cover, // 恢复原来的fit
                              //   ),
                              // ),
                              // 灵条和日柱文字 - 竖着显示在标签内部（逐字竖排，保持正向）
                              Positioned(
                                top: 60.h,
                                left: 0,
                                right: 0,
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    // 灵条 - 竖着显示
                                    if (_data?.lingTiao.isNotEmpty == true)
                                      Container(
                                        margin: EdgeInsets.only(right: 8.w),
                                        child: _buildVerticalText(
                                          text: _data!.lingTiao,
                                          color: Colors.white,
                                          fontSize: 16.sp,
                                          fontWeight: FontWeight.w500,
                                          letterSpacing: 2.h,
                                          strokeColor: const Color(0xFFFF6B35),
                                          strokeWidth: 1.2,
                                        ),
                                      ),
                                    // 日柱 - 竖着显示
                                    if (_data?.sunPillar.isNotEmpty == true)
                                      _buildVerticalText(
                                        text: _data!.sunPillar,
                                        color: Colors.white,
                                        fontSize: 16.sp,
                                        fontWeight: FontWeight.w500,
                                        letterSpacing: 2.h,
                                        strokeColor: const Color(0xFFFF6B35),
                                        strokeWidth: 1.2,
                                      ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),

                      // 内容区域
                      Center(
                        child: Container(
                          width: double.infinity,
                          height: double.infinity,
                          padding: EdgeInsets.only(top: 50.w),
                          child: Stack(
                            children: [
                              // 外层背景 - innerbg
                              Center(
                                child: Assets.signature.innerBg.image(
                                  width: double.infinity,
                                  height: double.infinity,
                                  fit: BoxFit.contain,
                                ),
                              ),

                              // 内层背景 - innerbg2
                              Center(
                                child: Assets.signature.innerBg2.image(
                                  width: double.infinity,
                                  height: double.infinity,
                                  fit: BoxFit.contain,
                                ),
                              ),

                              // 内容文字 - 添加滚动支持
                              Positioned.fill(
                                top: 70.w,
                                left: 40.w,
                                right: 40.w,
                                bottom: 30.h,
                                child: SingleChildScrollView(
                                  physics: const BouncingScrollPhysics(),
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      // 健康评语
                                      _buildSection(
                                        title: '健康评语',
                                        content: [
                                          _data?.healthReviews ?? '',
                                        ],
                                      ),

                                      _buildSeparator(),

                                      _buildSection(
                                        title: '健康建议',
                                        content: [
                                          _data?.healthAdvice ?? '',
                                        ],
                                      ),

                                      _buildSeparator(),

                                      // 健康溯源
                                      _buildSection(
                                        title: '健康溯源',
                                        content: [
                                          _data?.healthTraceability ?? '',
                                        ],
                                      ),

                                      _buildSeparator(),

                                      _buildSection(
                                        title: '趣味彩蛋',
                                        content: [
                                          _data?.funEasterEggs ?? '',
                                        ],
                                      ),

                                      SizedBox(height: 20.h), // 底部留白
                                    ],
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),

                SizedBox(height: 16.h),

                // 分享好运按钮
                Padding(
                  padding: EdgeInsets.only(bottom: 30.h),
                  child: GestureDetector(
                    onTap: () {
                      // TODO: 实现分享功能
                      Toast.show(
                        '分享功能开发中...',
                      );
                    },
                    child: Stack(
                      children: [
                        // 按钮背景图片
                        Assets.signature.shareBtn.image(
                          width: double.infinity,
                          height: 53.h,
                          fit: BoxFit.cover,
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),

          // 顶部光效 - 最顶层
          Positioned(
            top: -80,
            left: 0,
            right: 0,
            child: IgnorePointer(
              child: Assets.signature.topLight.image(
                  width: double.infinity,
                  fit: BoxFit.cover,
                  color:
                      const Color.fromARGB(121, 255, 121, 59).withOpacity(0.4)),
            ),
          ),
        ],
      ),
    );
  }

  /// 构建内容区块
  Widget _buildSection({
    required String title,
    required List<String> content,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title,
          style: TextStyle(
            color: Color(0xffF9F8F7),
            fontSize: 15.sp,
            fontWeight: FontWeight.w500,
          ),
        ),
        SizedBox(height: 8.h),
        ...content.map((text) => text.isEmpty
            ? SizedBox.shrink()
            : Padding(
                padding: EdgeInsets.only(bottom: 4.h),
                child: Text(
                  text,
                  style: TextStyle(
                    color: Color(0xff797676),
                    fontSize: 13.sp,
                    height: 1.5,
                  ),
                ),
              )),
      ],
    );
  }

  /// 构建分隔线
  Widget _buildSeparator() {
    return Container(
      margin: EdgeInsets.symmetric(vertical: 12.h),
      height: 1.h,
      color: Color(0xffC89A8E).withOpacity(0.2),
    );
  }

  /// 构建竖排文本（逐字竖排，保持正向）
  Widget _buildVerticalText({
    required String text,
    required Color color,
    required double fontSize,
    required FontWeight fontWeight,
    double letterSpacing = 2.0,
    Color? strokeColor,
    double strokeWidth = 1.0,
  }) {
    final Characters chars = text.characters;
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: chars
          .map((String ch) => Padding(
                padding: EdgeInsets.only(bottom: letterSpacing),
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    if (strokeColor != null)
                      Text(
                        ch,
                        style: TextStyle(
                          foreground: Paint()
                            ..style = PaintingStyle.stroke
                            ..strokeWidth = strokeWidth
                            ..color = strokeColor,
                          fontSize: fontSize,
                          fontWeight: fontWeight,
                          height: 1.0,
                        ),
                      ),
                    Text(
                      ch,
                      style: TextStyle(
                        color: color,
                        fontSize: fontSize,
                        fontWeight: fontWeight,
                        height: 1.0,
                      ),
                    ),
                  ],
                ),
              ))
          .toList(),
    );
  }

  /// 获取今天的日期（农历格式）
  String _getTodayDate() {
    final now = DateTime.now();
    // 这里使用一个简单的农历转换，实际项目中可能需要更复杂的农历库
    // 暂时返回一个示例格式
    final lunarMonths = [
      '正月',
      '二月',
      '三月',
      '四月',
      '五月',
      '六月',
      '七月',
      '八月',
      '九月',
      '十月',
      '冬月',
      '腊月'
    ];

    // 简单的农历日期计算（这里只是示例，实际需要更精确的算法）
    final lunarMonth = lunarMonths[(now.month - 1) % 12];
    final lunarDay = ((now.day - 1) % 30) + 1;

    // 天干地支年份（简化版本）
    final heavenlyStems = ['甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸'];
    final earthlyBranches = [
      '子',
      '丑',
      '寅',
      '卯',
      '辰',
      '巳',
      '午',
      '未',
      '申',
      '酉',
      '戌',
      '亥'
    ];

    final yearIndex = (now.year - 4) % 60; // 简化计算
    final stemIndex = yearIndex % 10;
    final branchIndex = yearIndex % 12;

    return '${heavenlyStems[stemIndex]}${earthlyBranches[branchIndex]}年 $lunarMonth${_convertToChineseNumber(lunarDay)}';
  }

  /// 将数字转换为中文数字
  String _convertToChineseNumber(int number) {
    if (number <= 0 || number > 31) return number.toString();

    final chineseNumbers = [
      '',
      '一',
      '二',
      '三',
      '四',
      '五',
      '六',
      '七',
      '八',
      '九',
      '十',
      '十一',
      '十二',
      '十三',
      '十四',
      '十五',
      '十六',
      '十七',
      '十八',
      '十九',
      '二十',
      '二十一',
      '二十二',
      '二十三',
      '二十四',
      '二十五',
      '二十六',
      '二十七',
      '二十八',
      '二十九',
      '三十',
      '三十一'
    ];

    return chineseNumbers[number];
  }
}
